var ctx=window.daijia.ctx;
var zhuancheyear,zhuanchemonths,zhuanchecompanyId;
var zhuanchexdData = new Array(12);
var zhuanchecjData = new Array(12);
var zhuanchejeData = new Array(12);
var zhuancheMonth="";
var zhuancheDay="";
var zhuancheHour="";	
$(function(){
	function zhuancheTableload(){
		
		$.ajax({
			
	        url : ctx+'/chart/zhuancheTableOrderMonthData',
	        type:'GET',
	        async:true,
	        dataType : 'json',
	        data : {
	        
	        	"year":$("#zhuancheYear").val(),
	        	"companyId":$("#zhuancheCompanyId").val()
	        },
	        success : function(result){
	        	var head = "<tr style='font-size:14px;'><th style='width:90px;'>分类\\月份</th><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th><th>7</th><th>8</th><th>9</th><th>10</th><th>11</th><th>12</th></tr>";
	        	var tr1='<td>销单量</td>';
	        	var tr2='<td>完成单量</td>';
	        	var tr3='<td>金额(元)</td>';
	        	$("#zhuancheHead tr").remove();
	        	$("#zhuancheHead").append(head);

	        	if(result.length>0){
					newcontent+="<tbody style='display:none;'>";
					console.debug(result);
					for(var i=0;i<12;i++){
						var cancel=0;
						var finish = 0;
						var money = 0;
                        for(var j= 0,val;val=result[j++];){

							if(i==val.time){
								cancel=val.cancelAmount;
								finish =val.finishedAmount;
								money =val.money;
								break;
							}
						}
							tr1+="<td>"+cancel+"</td>";
							tr2+="<td>"+finish+"</td>";
							tr3+="<td>"+money+"</td>"

					}

	       newcontent+="</tbody>";
	        	}
	        	var newcontent = "<tr>"+tr1+"</tr><tr>"+tr2+"</tr><tr>"+tr3+"</tr>";
	        	$('.zcloadblacklayer').remove();
	        	$("#zhuancheTable tbody").remove();
	        	$("#zhuancheTable").append(newcontent);
	        	$("#zhuancheTable tbody").show(300);
	    	}
		});
	}
	
	zhuancheTableload();
	
	
	
	$("#zhuanche_export_button").click(function(){
		window.location=ctx+"/chart/zhuancheExport?exportYear="+$("#zhuancheYear").val()+(isMain?"&exportCompanyId="+$("#zhuancheCompanyId").val():"")+
		 "&exportMonth="+zhuancheMonth+"&exportDay="+zhuancheDay;;
	});
	$("#zhuanche_search_button").click(function(){
		zhuancheyear = $("#zhuancheYear").val();
		zhuanchecompanyId = $("#zhuancheCompanyId").val();
		var errandMonth="";
		var errandDay="";
		var errandHour="";
		$("#zhuanche").css("width","100%");
		var y = $("#zhuancheYear").val();
		var c=$("#zhuancheCompanyId").val();
		var cname =$('#zhuancheCompanyId').find("option:selected").text();
		if($('.zcloadblacklayer').length==0){
			var dw = $("#zhuancheTab").width();
			var dh = $("#zhuancheTab").height();
			var backlayer ="<div class='zcloadblacklayer'><div style='margin-top:"+(dh/2)+"px;margin-left:"+(dw/2)+"px;'><img src='"+ctx+"/assets/css/images/loading.gif' /></div></div>"
			$("#zhuancheTab").append(backlayer);
			$('.zcloadblacklayer').css({"z-index":"12","background":"#000","opacity":"0.5","position":'absolute',"top":0,"width":dw,"height":dh});
		}
		$.post(window.daijia.ctx+'/chart/zhuancheDataForYear',{'year':y,'companyId':c},function(data){
			$('.zcloadblacklayer').remove();
			zhuanchemonths = data.months;
              if(zhuanchemonths != null && zhuanchemonths.length>0){
				  for(var i=0 ; i<zhuanchemonths.length ; i++){
					  var month = zhuanchemonths[i];
					  zhuanchexdData[month.month] = month.totalCancelNum;
					  zhuanchecjData[month.month] = month.totalSuccessNum;
					  zhuanchejeData[month.month] = month.totalPrice;
				  }
			  }else{
				  for(var i=0 ; i<12 ; i++){
					  zhuanchexdData[i] = 0;
					  zhuanchecjData[i] = 0;
					  zhuanchejeData[i] = 0;
				  }
				  $.scojs_message('没有订单记录', $.scojs_message.TYPE_OK);
			  }
				zhuancheTableload();
				zhuancheRenderYearChart(zhuanchexdData,zhuanchecjData,zhuanchejeData,zhuanchemonths);
			
		},'json');
		
	});
	
	
	Highcharts.setOptions({
		lang:{
			printChart:'打印报表',
			downloadJPEG:'保存为JPG图片',
			downloadPDF: '保存为PDF文档',
			downloadPNG: '保存为PNG图片',
			downloadSVG: '保存为SVG文档',
			loading:'加载中...',
			resetZoom:'重置缩放',
			resetZoomTitle:'缩放比例：1:1',
			months:['1月', '2月', '3月', '4月', '5月', '6月','7月', '8月', '9月', '10月', '11月', '12月']
		}
	});
	
	zhuancheyear = $("#zhuancheYear").val();
	zhuanchecompanyId = $("#zhuancheCompanyId").val();
	
	for(var i=0 ; i<12 ; i++){
		zhuanchexdData[i] = 0;
		zhuanchecjData[i] = 0;
		zhuanchejeData[i] = 0;
	}
	
	
	$.post(window.daijia.ctx+'/chart/zhuancheDataForYear',{'year':zhuancheyear,'companyId':zhuanchecompanyId},function(data){
		
		zhuanchemonths = data.months;
		
		if(zhuanchemonths){
			for(var i=0 ; i<zhuanchemonths.length ; i++){
				var month = zhuanchemonths[i];
				zhuanchexdData[month.month] = month.totalCancelNum;
				zhuanchecjData[month.month] = month.totalSuccessNum;
				zhuanchejeData[month.month] = month.totalPrice;
			}
		}else{

			$.scojs_message('没有订单记录', $.scojs_message.TYPE_OK);
		}
		zhuancheRenderYearChart(zhuanchexdData,zhuanchecjData,zhuanchejeData,zhuanchemonths);
	},'json');
	
	/*$("#year").change(function(){
		$("#submit_form").submit();
	});
	
	$("#companyId").change(function(){
		$("#submit_form").submit();
	});*/
	
});

zhuancheRenderYearChart=function(xdData,cjData,jeData,months){
	$("#zhuanche").css("width",$(".container-fluid .navigation").width());
	$("#zhuanche").highcharts({
		chart: {
            renderTo: 'zhuanche',
            zoomType: 'xy'
        },
		plotOptions: {
			series:{
				cursor: 'pointer',
	            events:{
	            	click:function(e){
	            		$("#zhuanche").css("width","100%");
	            		var category = e.point.category;
	            		var month = parseInt(category.substring(0,category.length-1))-1;
	            		zhuancheMonth=month;
	            		$.post(window.daijia.ctx+'/chart/zhuancheDataForMonth',{'year':$("#zhuancheYear").val(),'month':month,"companyId":$("#zhuancheCompanyId").val()},function(data){
	            			console.debug(data);
	            			var dataHours = data.hours;
	            			var hours = new Array();
	            			var days=data.chartDays;
							var tempDays=[];
	            			var xdmData = new Array();
	            			var cjmData = new Array();
	            			var jemData = new Array();
	            			var head = "<tr style='font-size:14px;'><th style='width:90px;'>分类\\天</th>";
	            			var tr1='<td>销单量</td>';
	            			var tr2='<td>完成单量</td>';
	            			var tr3='<td>金额(元)</td>';
                            for(var j =1;j<=data.days;j++){
								var cancel=0;
								var finish = 0;
								var money = 0;
								tempDays.push(j);
                                /*for(var i= 0,day;day=days[i++];){
                                    if(j==day.day){
                                        cancel=day.totalCancelNum;
                                        finish =  day.totalSuccessNum;
                                        money =  day.totalPrice;

                                        break;
                                    }
                                }
                               head+="<th>"+j+"</th>";
                               tr1+="<td>"+cancel+"</td>";
                               tr2+="<td>"+finish+"</td>";
                               tr3+="<td>"+money+"</td>";
                               xdmData[j-1] =cancel;
                               cjmData[j-1] = finish;
                               jemData[j-1] = money;
                               */
                           }
                           for(var i = 0 ; i < days.length ; i++){
                               var chartDay = days[i];
                               days[i] = chartDay.day;
                               xdmData[i] = chartDay.totalCancelNum;
                               cjmData[i] = chartDay.totalSuccessNum;
                               jemData[i] = chartDay.totalPrice;
                               head+="<th>"+chartDay.day+"</th>";
                               tr1+="<td>"+chartDay.totalCancelNum+"</td>";
                               tr2+="<td>"+chartDay.totalSuccessNum+"</td>";
                               tr3+="<td>"+chartDay.totalPrice+"</td>";
                           }
	            			head=head+"</tr>"
	            			var newcontent = "<tr>"+tr1+"</tr><tr>"+tr2+"</tr><tr>"+tr3+"</tr>";
	            			$("#zhuancheHead tr").remove();
	            			$("#zhuancheHead").append(head);
	            			$("#zhuancheTable tbody").remove();
	            			$("#zhuancheTable").append(newcontent);
	            			$("#zhuancheTable tbody").show(300);
		            		
	            			zhuancheRenderMonthChart(zhuancheyear,month,tempDays,xdmData,cjmData,jemData);
	            			
	            		},'json');
/*	            		var category = e.point.category;
	            		//加载月数据 
	            		var year = $("#year").val();
	            		var month = parseInt(category.substring(0,category.length-1))-1;
	            		var days = new Array();
	            		var xdmData = new Array();
	            		var cjmData = new Array();
	            		var jemData = new Array();
	            		
            			var currentMonth = months[month];
            			var days = currentMonth.chartDays;
            			for(var i=0;i<mouths.length;i++){
            				
            				xdmData[i]=mouths[i].totalCancelNum;
            				cjData[i] = mouths[i].totalSuccessNum;
            				jemData[i]=mouths[i].totalPrice;
            				
            			}
            			
            			
            			for(var i = 0 ; i < days.length ; i++){
            				var chartDay = days[i];
            				days[i] = chartDay.day;
            				xdmData[i] = chartDay.totalCancelNum;
            				cjmData[i] = chartDay.totalSuccessNum;
            				jemData[i] = chartDay.totalPrice;
            			}
            			
	            		
            			errandRenderMonthChart(year,month,days,xdmData,cjmData,jemData);*/
	            	}
	            }
			}
        },
        credits:{
        	enabled:false
        },
        title: {
            text: '专车订单统计图'
        },
        subtitle: {
            text: zhuancheyear+'年'
        },
        xAxis: [{
            categories: Highcharts.getOptions().lang.months
        }],
        yAxis: [{ // Primary yAxis
            labels: {
                format: '{value}单',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            title: {
                text: '单量',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            allowDecimals: false
        }, { // Secondary yAxis
            title: {
                text: '总价',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            labels: {
                format: '{value} 元',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            opposite: true
        }],
        tooltip: {
            shared: true
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 120,
            verticalAlign: 'top',
            y: 100,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
        },
        series: [{
            name: '金额',
            type: 'column',
            yAxis: 1,
            data: jeData,
            tooltip: {
                valueSuffix: '元'
            }

        }, {
            name: '单量',
            type: 'spline',
            data: cjData,
            tooltip: {
                valueSuffix: '单'
            }
        }, {
            name: '已销单',
            type: 'spline',
            data: xdData,
            tooltip: {
                valueSuffix: '个'
            }
        }]
    });
	
};


zhuancheRenderMonthChart=function(year,month,days,xdData,cjData,jeData){
	
	$("#zhuanche").highcharts({
		chart: {
            renderTo:  'zhuanche',
            zoomType: 'xy'
        },
		plotOptions: {
			series:{
				cursor: 'pointer',
	            events:{
	            	click:function(e){
	            		var category = e.point.category;
	            		//加载某天数据 
	            		
	            		zhuancheDay=category;
	            		$.post(window.daijia.ctx+'/chart/zhuancheDataForDay',{'year':year,'month':month,'day':category,"companyId":$("#zhuancheCompanyId").val()},function(data){
	            			
	            			var dataHours = data.hours;
	            			var hours = new Array();
	            			var xddData = new Array();
	            			var cjdData = new Array();
	            			var jedData = new Array();
	            			var head = "<tr style='font-size:14px;'><th style='width:90px;'>分类\\小时</th>";
	            			var tr1='<td>销单量</td>';
	            			var tr2='<td>完成单量</td>';
	            			var tr3='<td>金额(元)</td>';
	            			for(var i=0 ; i<dataHours.length ; i++){
	            				var hour = dataHours[i];
	            				hours[i] = hour.hour;
	            				xddData[i] = hour.totalCancel;
	            				cjdData[i] = hour.totalSuccess;
	            				jedData[i] = hour.totalPrice;
	            				head+="<th>"+hour.hour+"</th>";
	            				tr1+="<td>"+hour.totalCancel+"</td>";
	            				tr2+="<td>"+hour.totalSuccess+"</td>";
	            				tr3+="<td>"+hour.totalPrice+"</td>";
	            			}
	            			head=head+"</tr>"
	            			var newcontent = "<tr>"+tr1+"</tr><tr>"+tr2+"</tr><tr>"+tr3+"</tr>";
	            			$("#zhuancheHead tr").remove();
	            			$("#zhuancheHead").append(head);
	            			$("#zhuancheTable tbody").remove();
	            			$("#zhuancheTable").append(newcontent);
	            			$("#zhuancheTable tbody").show(300);
	            			
	            			zhuancheRenderDayChart(year,month,category,hours,xddData,cjdData,jedData);
	            			
	            		},'json');
	            		
	            	}
	            }
			}
        },
        credits:{
        	enabled:false
        },
        title: {
            text: '专车订单统计图'
        },
        subtitle: {
            text: zhuancheyear+"年-"+(month+1)+"月"
        },
        xAxis: [{
            categories: days
        }],
        yAxis: [{ // Primary yAxis
            labels: {
                format: '{value}单',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            title: {
                text: '单量',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            allowDecimals: false
        }, { // Secondary yAxis
            title: {
                text: '总价',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            labels: {
                format: '{value} 元',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            opposite: true
        }],
        tooltip: {
            shared: true
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 120,
            verticalAlign: 'top',
            y: 100,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
        },
        series: [{
            name: '金额',
            type: 'column',
            yAxis: 1,
            data: jeData,
            tooltip: {
                valueSuffix: '元'
            }

        }, {
            name: '单量',
            type: 'spline',
            data: cjData,
            tooltip: {
                valueSuffix: '单'
            }
        }, {
            name: '已销单',
            type: 'spline',
            data: xdData,
            tooltip: {
                valueSuffix: '个'
            }
        }]
    });
	
};

zhuancheRenderDayChart=function(year,month,day,hours,xddData,cjdData,jedData){
	
	$("#zhuanche").highcharts({
		chart: {
            renderTo: 'zhuanche',
            reflow:true,
            zoomType: 'xy'
        },
		plotOptions: {
			series:{
				cursor: 'pointer',
	            events:{
	            	click:function(e){
	            		//TODO 返回年数据统计
	            		zhuancheDay="";
	            		zhuancheMonth="";
	            		zhuancheHour="";
	            		var head = "<tr style='font-size:14px;'><th style='width:90px;'>分类\\月份</th>";
            			var tr1='<td>销单量</td>';
            			var tr2='<td>完成单量</td>';
            			var tr3='<td>金额(元)</td>';
						for(var j=0;j<12;j++){
							var cancel = 0;
							var finish = 0;
							var money = 0;
							for(var i=0;i<zhuanchemonths.length;i++){
								if(j==zhuanchemonths[i].month){
									cancel = zhuanchemonths[i].totalCancelNum;
									finish = zhuanchemonths[i].totalSuccessNum;
									money = zhuanchemonths[i].totalPrice;
								}
							}
							head+="<th>"+(j+1)+"</th>";
							tr1+="<td>"+cancel+"</td>";
							tr2+="<td>"+finish+"</td>";
							tr3+="<td>"+money+"</td>";
						}

	            		/*for(var i=0;i<zhuanchemonths.length;i++){
	            			head+="<th>"+((zhuanchemonths[i].month)+1)+"</th>";
	            			tr1+="<td>"+(zhuanchemonths[i]).totalCancelNum+"</td>";
            			    tr2+="<td>"+(zhuanchemonths[i]).totalSuccessNum+"</td>";
            			    tr3+="<td>"+(zhuanchemonths[i]).totalPrice+"</td>";
	            		}*/
	            		head=head+"</tr>"
            			var newcontent = "<tr>"+tr1+"</tr><tr>"+tr2+"</tr><tr>"+tr3+"</tr>";
            			$("#zhuancheHead tr").remove();
            			$("#zhuancheHead").append(head);
            			$("#zhuancheTable tbody").remove();
            			$("#zhuancheTable").append(newcontent);
            			$("#zhuancheTable tbody").show(300);
	            		zhuancheRenderYearChart(zhuanchexdData,zhuanchecjData,zhuanchejeData,zhuanchemonths);
	            	}
	            }
			}
        },
        credits:{
        	enabled:false
        },
        title: {
            text: '专车订单统计图'
        },
        subtitle: {
            text: zhuancheyear+"年-"+(month+1)+"月-"+day+"日"
        },
        xAxis: [{
            categories: hours
        }],
        yAxis: [{ // Primary yAxis
            labels: {
                format: '{value}单',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            title: {
                text: '单量',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            allowDecimals: false
        }, { // Secondary yAxis
            title: {
                text: '总价',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            labels: {
                format: '{value} 元',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            opposite: true
        }],
        tooltip: {
            shared: true
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 120,
            verticalAlign: 'top',
            y: 100,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
        },
        series: [{
            name: '金额',
            type: 'column',
            yAxis: 1,
            data: jedData,
            tooltip: {
                valueSuffix: '元'
            }

        }, {
            name: '单量',
            type: 'spline',
            data: cjdData,
            tooltip: {
                valueSuffix: '单'
            }
        }, {
            name: '已销单',
            type: 'spline',
            data: xddData,
            tooltip: {
                valueSuffix: '个'
            }
        }]
    });
	
};